package com.dangf.hzapp.activity;

import java.util.HashMap;
import java.util.Map;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dangf.hzapp.Config;
import com.dangf.hzapp.R;
import com.dangf.hzapp.util.Http;
import com.dangf.hzapp.util.Http.Callback;
import com.dangf.hzapp.util.Http.Method;
import com.dangf.hzapp.util.Image;

/**
 * 注册界面
 * @author 韦海生
 * @date 2014年8月2日
 */
public class RegisterActivity extends BaseActivity {
	private ImageView user_photo;	//头像
	private Button register_button;	//注册按钮
	private EditText username_edit;	//用户名
	private EditText password_edit;	//密码

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		
		//初始化控件
		user_photo = (ImageView) findViewById(R.id.user_photo);
		username_edit = (EditText) findViewById(R.id.username);
		password_edit = (EditText) findViewById(R.id.password);
		register_button = (Button) findViewById(R.id.register_button);
		
		//设置图片
		Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
		user_photo.setImageBitmap(Image.getRoundedCornerBitmap(bitmap,2));
		
		initListener();
	}
	
	
	
	//初始化监听器
		private void initListener() {
			
			//登录按钮监听
			register_button.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if("".equals(username_edit.getText()+"") || "".equals(password_edit.getText()+"")){
						Toast.makeText(RegisterActivity.this, "账号密码不能为空！", Toast.LENGTH_SHORT).show();
					}else{
						//组合请求数据
						Map<String, String> data = new HashMap<String, String>();
						data.put("a", "User");
						data.put("m", "register");
						data.put("username", username_edit.getText()+"");
						data.put("password", password_edit.getText()+"");
						//开始请求
						showDialog("正在注册.");
						new Http(Config.HOST_URL, Method.GET, data , new Callback() {
							@Override
							public void onSuccess(String result) {
								try {
									hideDialog();
									//解释JSON
									JSONObject json = JSON.parseObject(result);
									if("200".equals(json.getString("code"))){
										
										Toast.makeText(RegisterActivity.this, json.getString("msg"), Toast.LENGTH_SHORT).show();
										Intent i = new Intent();
										i.putExtra("username", username_edit.getText()+"");
										i.putExtra("password", password_edit.getText()+"");
										setResult(200, i);	//传值到上一个 Activity
										finish();
									}else{
										Toast.makeText(RegisterActivity.this, json.getString("msg"), Toast.LENGTH_SHORT).show();
									}
								} catch (Exception e) {
									Toast.makeText(RegisterActivity.this,"请求失败，请检查网络状态。", Toast.LENGTH_SHORT).show();
								}
								
							}
						});
						
					}
				}
			});
		}
		
		

	@Override
	protected String getActivityName() {
		return "注册";
	}
}
